package com.neoCalf.neoNotify.config;

import com.neoCalf.neoCommon.redis.RedisTool;
import com.neoCalf.neoCommon.utils.CodeUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/12 23:43
 */
@Configuration
public class CommonConfig<V> {
    @Bean
    public CodeUtils codeUtils(RedisTool<String> redisTool) {
        return new CodeUtils(redisTool);
    }

    @Bean
    public RedisTool<V> redisTool(RedisTemplate<String, V> redisTemplate) {
        return new RedisTool<>(redisTemplate);
    }

    @Bean("redisTemplate")
    public RedisTemplate<String, V> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, V> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        //设置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        //设置value的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        //设置hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        //设置hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());
        return template;
    }
}
